#include "engrave1.h"
// This is included in engrave.cpp and is control dependant.
// This one is for dad's series one Brigeport milling machine

// it was changed to use the parallel port on 16th September 2004, ,because of unsurmountable reliability problems with John Trevellyan's old IO card

//#define LOG_FILE

char global_specs_file_name[] = "series1.txt";

#ifdef LOG_FILE
FILE *log_stream = NULL;
long step_val[3] = {0, 0, 0};
long cycles_between[3] = {0, 0, 0};
long min_cycles[3] = {1000, 1000, 1000};
int first_time[3] = {1, 1, 1};
#endif

char message[256];

#ifdef LOG_FILE
void put_cycles(int axis){
	sprintf(message, "(%ld)", cycles_between[axis]);
	fputs(message, log_stream);
	if(!first_time[axis]){
		if(cycles_between[axis]<min_cycles[axis])min_cycles[axis] = cycles_between[axis];
		if(cycles_between[axis] == 2){
			cycles_between[axis] = 2;
		}
	}
	cycles_between[axis] = 0;
	first_time[axis] = 0;
}
#endif

void move(int axis, int plus)
{
	if(axis==0)
	{
		// move the X-AXIS

#ifdef LOG_FILE
		if(plus){
			fputs("X1", log_stream);
			step_val[0]++;
		}
		else{
			fputs("X0", log_stream);
			step_val[0]--;
		}
		put_cycles(0);
#else
		if(plus)globals.output_step0++;
		else globals.output_step0--;
		if(globals.output_step0<0)globals.output_step0+=4;
		if(globals.output_step0>3)globals.output_step0-=4;

		globals.output_value &= 0xfc;

		if(globals.output_step0 == 0)
		{
			globals.output_value |= 0;
		}
		else if(globals.output_step0 == 1)
		{
			globals.output_value |= 2;
		}
		else if(globals.output_step0 == 2)
		{
			globals.output_value |= 3;
		}
		else if(globals.output_step0 == 3)
		{
			globals.output_value |= 1;
		}
		outp(global_parallel_port,globals.output_value);
#endif
	}
	else if(axis==1)
	{
		// move the Y-AXIS

#ifdef LOG_FILE
		if(plus)fputs("Y1", log_stream);
		else fputs("Y0", log_stream);
		if(plus)step_val[1]++;
		else step_val[1]--;
		put_cycles(1);
#else
		if(plus)globals.output_step1++;
		else globals.output_step1--;
		if(globals.output_step1<0)globals.output_step1+=4;
		if(globals.output_step1>3)globals.output_step1-=4;

		globals.output_value &= 0xf3;

		if(globals.output_step1 == 0)
		{
			globals.output_value |= 0;
		}
		else if(globals.output_step1 == 1)
		{
			globals.output_value |= (2<<2);
		}
		else if(globals.output_step1 == 2)
		{
			globals.output_value |= (3<<2);
		}
		else if(globals.output_step1 == 3)
		{
			globals.output_value |= (1<<2);
		}
		outp(global_parallel_port,globals.output_value);
#endif
	}
	else if(axis==2)
	{
		// move the Z-AXIS

#ifdef LOG_FILE
		if(plus)fputs("Z1", log_stream);
		else fputs("Z0", log_stream);
		if(plus)step_val[2]++;
		else step_val[2]--;
		put_cycles(2);
#else
		if(plus)globals.output_step2++;
		else globals.output_step2--;
		if(globals.output_step2<0)globals.output_step2+=4;
		if(globals.output_step2>3)globals.output_step2-=4;

		globals.output_value &= 0xcf;

		if(globals.output_step2 == 0)
		{
			globals.output_value |= 0;
		}
		else if(globals.output_step2 == 1)
		{
			globals.output_value |= 0x20;
		}
		else if(globals.output_step2 == 2)
		{
			globals.output_value |= 0x30;
		}
		else if(globals.output_step2 == 3)
		{
			globals.output_value |= 0x10;
		}
		outp(global_parallel_port,globals.output_value);
#endif
	}
}

int is_limit_switch_pressed(void){
	#ifdef LOG_FILE
return 0;
#else
	return (inp(global_parallel_port + 1) & 128) == 0;
#endif
}

int is_hold_button_pressed(void){
	#ifdef LOG_FILE
return 0;
#else
	return (inp(global_parallel_port + 1) & 64) != 0;
#endif
}

int is_z_top(void){
#ifdef LOG_FILE
	if(step_val[2]>100000)return 1;
	else return 0;
#else
	return (inp(global_parallel_port + 1) & 16) != 0;
#endif
}

int is_z_near(void){
	#ifdef LOG_FILE
	if(step_val[2]>90000)return 1;
	else return 0;
#else
	return (inp(global_parallel_port + 1) & 32) != 0;
#endif
}

void init_output(void){
#ifdef LOG_FILE
	/* open the log file to write */
	if((log_stream  = fopen( "movelog.txt", "wb" )) == NULL)
	{
		return;
	}
#else
	outp(global_parallel_port, globals.output_value);
	// ( globals.output_value is saved )
#endif
	}

void drill_onoff(int onoff){
	int temp = onoff;
	temp = temp;
}

void welcome_notice(){
	// welcome notice
	cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
	cout<<"     HEEKSATROL Copyright HEEKS MOTION CONTROL UK August 2001\n\n";
	cout<<"   Now please turn on your Bridgeport Series1 milling machine\n\n\n";
}

void goodbye_notice(){
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
cout<<"   Now turn off your Bridgeport Series1 milling machine\n\n";
cout<<"   Then turn off \"PC\" and \"MONITOR\" as fast as you like\n\n\n";
cout<<"                             Goodbye!\n\n\n\n\n\n\n\n\n";
cout<<"PS you can type \"edit specs.txt\" now if you want, but DO save before you finish\n";
#ifdef LOG_FILE
	cout<<"relative pos   X "<<step_val[0]<<", Y "<<step_val[1]<<", Z "<<step_val[2]<<"\n";
	cout<<"min cycles X "<<min_cycles[0]<<", Y "<<min_cycles[1]<<", Z "<<min_cycles[2]<<"\n";
	getch();
	fclose(log_stream);
#endif
}

void on_every_cycle(){
#ifdef LOG_FILE
	cycles_between[0]++;
	cycles_between[1]++;
	cycles_between[2]++;
#endif
}

int get_z_switches_type(){
return 1;
}

int backlash_graphs_used(void){
return 0;
}

void spindle_off(void)
{
		globals.output_value |= 0x40;
		outp(global_parallel_port,globals.output_value);
}

void spindle_off_cancel(void)
{
		globals.output_value &= 0xbf;
		outp(global_parallel_port,globals.output_value);
}



#include "engrave2.h"
